<?php
!defined('IN_FRAME') && die('404 Page');
class m_content extends Model {
    
    /*
    	title, desc, cat_id, source_from, thumb, tags, seo_t, seo_k, seo_d, search_index,
    	id, user_id, user_name, list_sort, is_top, sysadd, rec_status, created_at, last_updated
    */
    
    public function __construct() {
        $this->table = 't_content';
        parent::__construct();
    }
    
    public function getHots($intCnt = 10, $intFrom = 500, $_idCat = '', $idExp = 0) {
        $strWhere = '`view_cnt` > 0 AND `rec_status` > 0';
        $idExp > 0 && $strWhere .= ' AND `id` <> ' . $idExp;
        if (!empty($_idCat)) {
            $objCat = Helper::loadModel('cat')->objTreeList;
            if (isset($objCat->arrIdAll[$_idCat])) {
                $arrTemp = array($_idCat);
                if (isset($objCat->arrIdChildren[$_idCat])) {
                    $arrTemp = array_merge($arrTemp, $objCat->arrIdChildren[$_idCat]);
                }
                $strWhere .= ' AND `cat_id` IN (' . str_replace('_', '', implode(', ', $arrTemp)) . ')';
            }
        }
        $arrData = $this->select($strWhere, array('id', 'view_cnt', 'title', 'cat_id', 'created_at'), 'ORDER BY `created_at` DESC LIMIT 0, ' . $intFrom);
        $intNow = time();
        if ($arrData) {
            $arrHot = array();
            foreach ($arrData as $intTemp => $arrTemp) {
                unset($arrData[$intTemp]);
                $intTemp = $intNow - strtotime($arrTemp['created_at']);
                $intTemp = ($intTemp / 3600) + 2;
                $strTemp = '_' . $arrTemp['id'];
                $arrData[$strTemp] = $arrTemp;
                $arrHot[$strTemp] = doubleval($arrTemp['view_cnt'] / pow($intTemp, 1.8));
            }
            arsort($arrHot);
            $arrHot = array_keys($arrHot);
            $arrTemp = array();
            $intTemp = 0;
            foreach ($arrHot as $strTemp) {
                $intTemp ++;
                if ($intTemp <= $intCnt) {
                    array_push($arrTemp, $arrData[$strTemp]);
                } else {
                    break;
                }
            }
            $arrData = $arrTemp;
        }
        return $arrData;
    }
    
}